<template>
  <div>
    <!-- 头部导航栏 -->
    <!-- <van-nav-bar
              title 标题
              left-text 左箭头文字
              left-arrow 左箭头
              @click-left左箭头点击事件
 -->
    <van-nav-bar left-arrow @click-left="leftNav"></van-nav-bar>
    <div class="login-form">
      <h2 class="title">短信登录</h2>
      <van-form ref="form">
        <!-- 表单验证:  :rules -->
        <!-- 局部表单验证加name值 -->
        <van-field
          name="mobile"
          v-model="form.mobile"
          :rules="rules.mobile"
          placeholder="请输入手机号"
        ></van-field>
        <van-field
          v-model="form.code"
          :rules="rules.code"
          placeholder="请输入验证码"
        >
          <template #button>
            <div class="code" v-if="totalTime === 6" @click="sendCode">
              发送验证码
            </div>
            <div class="code" v-else>{{ totalTime }}秒后再试</div>
          </template>
        </van-field>
        <van-button class="btn" @click="submit">登录</van-button>
      </van-form>
    </div>
  </div>
</template>

<script>
import { authorizations, smsCodes } from '@/api/login.js'
export default {
  data () {
    return {
      interval: '',
      totalTime: 6,
      form: {
        mobile: '13200000000',
        code: '246810'
      },
      rules: {
        mobile: [
          {
            required: true,
            message: '必填',
            trigger: 'onBlur'
          },
          {
            validator: value => {
              const res = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              return res.test(value)
            },
            message: '请正确输入手机号',
            trigger: 'onBlur'
          }
        ],
        code: [
          { required: true, message: '必填', trigger: 'onBlur' },
          {
            validator: value => {
              return value.length === 6
            },
            message: '验证码长度为6',
            trigger: 'onBlur'
          }
        ]
      }
    }
  },
  methods: {
    leftNav () {
      // go(-1)去上一页
      this.$router.go(-1)
      // this.$router.push('/')
    },
    submit () {
      this.$refs.form.validate().then(async () => {
        this.$toast.loading({ duration: 0 })
        const res = await authorizations(this.form)
        this.$store.commit('login/setToken', res.data.token)
        // 储存refresh_token
        this.$store.commit('login/setRefreshToken', res.data.refresh_token)
        // 组件路由传值 基本写法  传:this.$router.push('/path?参数名'+值)  this.$router.push({path:'/path',query:{参数名:值}})
        //  接收值 this.$router.query.参数名
        const redirect = this.$route.query.redirect || '/'
        this.$router.push(redirect)
        this.$toast.success('登录成功')
        // console.log(res)
      })
      // .catch(() => {
      //   this.$toast.fail('验证失败')
      // })
    },
    sendCode () {
      this.$refs.form.validate('mobile').then(async () => {
        this.totalTime--
        this.interval = setInterval(() => {
          this.totalTime--
          if (this.totalTime <= 0) {
            clearInterval(this.interval)
            this.totalTime = 6
          }
        }, 1000)
        await smsCodes(this.form.mobile)
        this.$toast.success('验证码发送成功')
      })
      // .catch(() => {
      //   this.$toast.fail('验证失败')
      // })
    }
  },
  // 中断渲染
  beforeDestroy () {
    clearInterval(this.interval)
  }
}
</script>

<style lang="less" scoped>
::v-deep .van-nav-bar .van-icon {
  color: #999;
}
.code {
  color: #999;
}
.login-form {
  padding: 20px;
}
.title {
  font-weight: normal;
  margin-bottom: 40px;
  padding-left: 18px;
  font-size: 18px;
}
.btn {
  width: 100%;
  // background-color: #fe4f4e;
  background: linear-gradient(to right, #fe4f4e, #fc6628);
  color: #fff;
  margin-top: 30px;
}
</style>
